package com.example.administrator.caigou51.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.UpdateShopCarFragment;
import com.example.administrator.caigou51.EventBusClass.UpdateShopCarRedCount;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.CollectBean;
import com.example.administrator.caigou51.bean.MainDataBean;
import com.example.administrator.caigou51.bean.ShopCarBean;
import com.example.administrator.caigou51.control.DialogGoodsBuy;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListAdapter;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.AddAgainGoodsCard;
import com.example.administrator.caigou51.recyclerCard.card.CollectGoodsCard;
import com.example.administrator.caigou51.recyclerCard.card.ShopCarCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.widget.dialog.SweetDialogManager;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/9/6/006.
 */
public class CollectActivity extends GBaseActivity {
    MaterialListView material_listview;
    LinearLayout linearLayoutEmpty;
    List<CollectBean> collectBeanList;
    ImageView imageViewAll;
    TextView textViewCancel;
    TextView textViewJoinCar;
    private boolean isAllCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.activity_collect);
        findViewByIds();
        initDatas();
        TaskGetCollectData(false);
        EventBus.getDefault().register(this);
    }

    private void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
        imageViewAll= (ImageView) findViewById(R.id.imageViewAll);imageViewAll.setOnClickListener(this);
        linearLayoutEmpty= (LinearLayout) findViewById(R.id.linearLayoutEmpty);linearLayoutEmpty.setVisibility(View.GONE);
        textViewCancel= (TextView) findViewById(R.id.textViewCancel);textViewCancel.setOnClickListener(this);
        textViewJoinCar= (TextView) findViewById(R.id.textViewJoinCar);textViewJoinCar.setOnClickListener(this);
    }
    private void initDatas() {
        getBottomBar().setVisibility(View.VISIBLE);
        bottomButtonDefault();
        setTopTitle("收藏清单");


    }
    private void fillArrayInListView( List<CollectBean> collectBeanList) {
        material_listview.clear();
        for (int i=0;i<collectBeanList.size();i++) {
            CollectGoodsCard collectGoodsCard=new CollectGoodsCard(this);
            collectGoodsCard.setCollectBean(collectBeanList.get(i));
            collectGoodsCard.setImageViewAll(imageViewAll);
            material_listview.add(collectGoodsCard);
        }

    }

    private void TaskGetCollectData(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), material_listview, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_Collect, Constant.Action.Action_Collect, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_Collect, App.getUserBean().getUserid(),App.getUserBean().getSession_id()), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        collectBeanList = JSON.parseArray(result.getData().toString(), CollectBean.class);
                        App.saveCollectBeanListServer(collectBeanList);
                        fillArrayInListView(collectBeanList);
                        Log.e("userBean",result.getData().toString());
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    private void TaskDelCollectData(String ids) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), material_listview, false, null,
                Constant.getRootUrl() + Constant.Action.Action_Del_Collect, Constant.Action.Action_Del_Collect, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_Del_Collect, App.getUserBean().getUserid(),App.getUserBean().getSession_id(),ids), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        TaskGetCollectData(false);
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    private String getCheckIDS() {
        String ids="";
        MaterialListAdapter materialListAdapter= (MaterialListAdapter) material_listview.getAdapter();
        for (int i=0;i<materialListAdapter.getAllList().size();i++)
        {
            CollectGoodsCard collectGoodsCard= (CollectGoodsCard) materialListAdapter.getAllList().get(i);

                if (collectGoodsCard.getCollectBean().isCheck())
                    ids+=collectGoodsCard.getCollectBean().getCollect_id()+",";

        }
        if (!TextUtils.isEmpty(ids)) {
            ids = ids.substring(0, ids.length() - 1);
        }
        return ids;
    }
    public static void TaskGetShopCarData(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY(null, null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_GetCarList, Constant.Action.Action_GetCarList, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_GetCarList,App.getUserBean().getUserid(),App.getUserBean().getSession_id()), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        List<ShopCarBean> shopCarBeanList = JSON.parseArray(result.getData().toString(), ShopCarBean.class);
                        App.getShopCarList().clear();
                        App.getShopCarList().addAll(shopCarBeanList);
                        App.saveShopCarList(shopCarBeanList);

                        //刷新所有购物车的红点
                        EventBus.getDefault().post(new UpdateShopCarRedCount());
                        EventBus.getDefault().post(new UpdateShopCarFragment());

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textViewCancel:
                if (!TextUtils.isEmpty(getCheckIDS())) {
                    TaskDelCollectData(getCheckIDS());
                }
                break;
            case R.id.textViewJoinCar:
                if (collectBeanList!=null && !TextUtils.isEmpty(getCheckIDS()))
                {

                    for (int i=0;i<collectBeanList.size();i++)
                    {
                        CollectBean collectBean=collectBeanList.get(i);
                        if (collectBean.isCheck())
                        {
                                DialogGoodsBuy.TaskAddCar("1",null,(GBaseActivity) getActivity(),collectBean.getItemid(),true);
                        }
                    }
                }
                break;
            case R.id.imageViewAll:
                if (isAllCheck) {
                    for (int i = 0; i < collectBeanList.size(); i++) {
                        collectBeanList.get(i).setCheck(false);
                        material_listview.getAdapter().notifyDataSetChanged();
                    }

                    isAllCheck=false;
                    imageViewAll.setImageResource(R.drawable.icon_circle_grey);
                }else
                {
                    for (int i = 0; i < collectBeanList.size(); i++) {
                        collectBeanList.get(i).setCheck(true);
                        material_listview.getAdapter().notifyDataSetChanged();
                    }
                    isAllCheck=true;
                    imageViewAll.setImageResource(R.drawable.icon_yes_green);
                }
                break;

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    public void onEventMainThread(UpdateShopCarRedCount updateShopCarRedCount) {

        updateBottomShopCarCount();
    }
}
